package com.huiyujinrong.qiangdanbao.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.utils.DataUtils;
import com.huiyujinrong.qiangdanbao.utils.WebViewActivity;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends AppCompatActivity implements RegisterContract.View {


    @BindView(R.id.toolbar_Title)
    TextView title;
    @BindView(R.id.toolbar_Back)
    RelativeLayout back;
    @BindView(R.id.rg_pwd)
    EditText rg_pwd;
    @BindView(R.id.rg_tell)
    EditText rg_tell;
    @BindView(R.id.rg_code)
    EditText rg_code;
    @BindView(R.id.rg_pwd_sh)
    ImageView pwd_sh;
    @BindView(R.id.rg_agree)
    CheckBox rg_agree;

    private boolean isShow = true;

    @BindView(R.id.rg_sendCode)
    TextView senCode;

    private RegisterActivity.TimeCount time;

    private RegisterPresenter presenter;

    private final int CODE_TYPE = 1;

    private Toast t;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        time = new RegisterActivity.TimeCount(60000, 1000);
        inSetToolBar();
        presenter = new RegisterPresenter(this, this);
        presenter.start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.end();
    }

    private void inSetToolBar() {
        back.setVisibility(View.VISIBLE);
        title.setText("注 册");
    }

    @OnClick(R.id.toolbar_Back)
    void onBack() {
        finish();
    }


    //  显示明文密码，隐藏密码
    @OnClick(R.id.rg_pwd_s_h)
    void setSH() {
        if (isShow) {
            rg_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            pwd_sh.setBackgroundResource(R.drawable.btn_zc_hide);
            isShow = false;
        } else {
            rg_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
            pwd_sh.setBackgroundResource(R.drawable.btn_zc_show);
            isShow = true;
        }
        //切换后将EditText光标置于末尾
        CharSequence charSequence = rg_pwd.getText();
        if (charSequence != null) {
            Selection.setSelection((Spannable) charSequence, charSequence.length());
        }
        //刷新界面
        rg_pwd.postInvalidate();
    }

    //发送验证码
    @OnClick(R.id.rg_sendCode)
    void inSendCode() {
        if (DataUtils.getCheckTel(rg_tell.getText().toString())) {
            time.start();
            presenter.sendCode(rg_tell.getText().toString(), CODE_TYPE);
        } else {
            showToast("手机号不正确");
        }

    }


    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    //注册
    @OnClick(R.id.rg_register)
    void inRegister() {
        if (DataUtils.getCheckTel(rg_tell.getText().toString())) {
            if (TextUtils.isEmpty(rg_pwd.getText().toString()) && rg_pwd.getText().length() < 6) {
                showToast("密码必须大于6位");
            } else {
                if (TextUtils.isEmpty(rg_code.getText().toString()) && rg_code.getText().length() < 6) {
                    showToast("验证码有误");
                } else {
                    if (rg_agree.isChecked()) {
                        presenter.register(rg_tell.getText().toString(), rg_code.getText().toString(), rg_pwd.getText().toString());
                    } else {
                        showToast("需同意本平台协议");
                    }
                }
            }
        } else {
            showToast("手机号不正确");
        }
    }

    @Override
    public void showToast(String str) {
        if (t == null) {
            t = Toast.makeText(this, str, Toast.LENGTH_SHORT);
        } else {
            t.setText(str);
            t.setDuration(Toast.LENGTH_SHORT);
        }
        t.show();
    }

    @Override
    public void exit() {
        finish();
    }


    @OnClick(R.id.llAgreement)
    void RG_AGGREE() {
//        rg_agree.setChecked(true);
        Bundle bundle = new Bundle() ;
        bundle.putString("url", "http://106.15.102.162/jqb-api/qdb_rules/reg.html");
        bundle.putInt("mm", 3);
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 获取验证码  倒计时
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {// 计时完毕
            senCode.setText(getString(R.string.sendCode));
//            senCode.setBackgroundResource(R.drawable.shape_login_showcode);
            senCode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {// 计时过程
            senCode.setClickable(false);//防止重复点击
            senCode.setText(millisUntilFinished / 1000 + "s后重发");
        }
    }
}
